<?php


namespace sinri\wenli;


class DutyForLZZ extends PageReader
{
    protected $bookMapping = [];

    public function duty()
    {
        $this->fetchBookMapping();

        foreach ($this->bookMapping as $bookIndex => $bookMeta) {
            echo "Book Mapping {$bookIndex}th Item: " . json_encode($bookMeta) . PHP_EOL;
            $chapterCountForBook = $this->bookChapterMapping($bookIndex);
//            for($chapter=1;$chapter<=$chapterCountForBook;$chapter++) {
            //$url=$this->getBookURL($bookMeta['book_id'], $bookMeta['book_name'], $chapter);
            $this->dutyForBookByEachChapter($bookMeta['book_id'], $bookMeta['book_name'], $chapterCountForBook);
//            }
        }
    }

    public function version()
    {
        return "lzz";
    }

    protected function getBookURL($book_id, $book_name = null, $chapter_id = null)
    {
        $prefix = "/";
        if ($chapter_id < 10) {
            $prefix = "/00";
        } elseif ($chapter_id < 100) {
            $prefix = "/0";
        }
        $url = "http://www.cclw.net/Bible/LzzBible/" . $book_id . $prefix . $chapter_id . ".html";

        echo "getBookURL from " . json_encode([$book_id, $chapter_id]) . " : " . PHP_EOL;
        echo $url . PHP_EOL;

        return $url;
    }

    protected function html2txt($html, $chapter = null)
    {
        // <font style="font-size: 9.5pt; color=#D63E64">1</font> 起初，上帝创造天地。<br>

        $html = mb_convert_encoding($html, "UTF-8", "GB2312");

        $content = "";
        if (preg_match_all('/(\d+)<\/font>([^<]*)<br/', $html, $matches)) {
            $verseCount = count($matches[0]);
            for ($verse = 0; $verse < $verseCount; $verse++) {
                $content .= $chapter . ":" . $matches[1][$verse] . " " . $matches[2][$verse] . PHP_EOL;
            }

            echo "html2txt for chapter " . json_encode($chapter) . " PARSED!" . PHP_EOL;
            echo $content . PHP_EOL;
        } else {
            echo "html2txt for chapter " . json_encode($chapter) . " CANNOT BE PARSED" . PHP_EOL;
        }

        return $content;
    }

    protected function fetchBookMapping()
    {
        $url = "http://www.cclw.net/Bible/LzzBible/img/list.html";
        $code = $this->readPage($url);
        $code = mb_convert_encoding($code, "UTF-8", "GB2312");
        //echo $code.PHP_EOL;

        if (preg_match_all('/[aA] HREF="..\/(\d+)\/[A-Za-z0-9.]*">([^<]+)<\/A/', $code, $matches)) {
//            var_dump($matches[1]);
//            var_dump($matches[2]);

            $this->bookMapping = [];

            $count = count($matches[0]);
            for ($i = 0; $i < $count; $i++) {
                $this->bookMapping[$i] = [
                    "book_id" => $matches[1][$i],
                    "book_name" => $matches[2][$i],
                ];
                //[$matches[1][$i]]=$matches[2][$i];
            }
            var_dump($this->bookMapping);
        }
    }

    protected function bookIdToReadIndex($bookId)
    {
        foreach ($this->bookMapping as $bookIndex => $bookMeta) {
            if ($bookId == $bookMeta['book_id']) {
                $x = $bookIndex + 1;
                return ($x < 10 ? "0" : "") . $x;
            }
        }
        return $bookId;
    }
}